package com.neo.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import com.mysql.jdbc.StringUtils;

public class DjsStringUtil {

    public static final String BLANK = " ";
    public static final String ZERO = "0";


    public static final String PRODUCT = "0111";
    //标的迁移
    public static final String COINSTCODE = "000226";
    public static final String BANKID = "3005";
    public static final String FUISSUER = "J5";

    //	
    //	/home/tomcat/transfer/
    public static final String FILE_PATH = "D:/点金石/新债权信息/req/";
    public static final String IMPORT_FILE_PATH = "D:/点金石/新债权信息/uat/债权信息.csv";
    public static final String PLAN_FILE_PATH = "D:/点金石/新债权信息/变更放款日期.csv";

    public static String fillDefault(String in, String defaultString) throws UnsupportedEncodingException {
        if (in == null)
            in = defaultString;
        return in;
    }

    public static String fillBlank(String in, int len) throws UnsupportedEncodingException {
        if (in == null)
            in = "";
        byte[] ins = in.getBytes("GBK");
        for (int i = 0; i < len - ins.length; i++) {
            in += BLANK;
        }
        return in;
    }

    //去空格
    public static String trimBlank(String in) throws UnsupportedEncodingException {
        if (in == null)
            in = "";
        in = in.replaceAll(" ", "");
        return in;
    }

    public static String trimFill(String in, Boolean numberFlag) {
        if (in == null) {
            in = "";
        }
        in = in.replaceAll(" ", "");
        if (numberFlag && StringUtils.isStrictlyNumeric(in)) {
            BigDecimal decimal = new BigDecimal("0." + in.substring(in.length() - 2));
            BigDecimal integer = new BigDecimal(in.substring(0, in.length() - 2));
            in = integer.add(decimal).toString();
        }
        return in;
    }

    public static String fillLeftZero(String in, int len) throws UnsupportedEncodingException {
        if (in == null)
            in = "";
        byte[] ins = in.getBytes("GBK");
        String s = "";
        for (int i = 0; i < len - ins.length; i++) {
            s += ZERO;
        }
        return s + in;
    }

    public static String fillRightZero(String in, int len) throws UnsupportedEncodingException {
        if (in == null)
            in = "";
        byte[] ins = in.getBytes("GBK");
        String s = "";
        for (int i = 0; i < len - ins.length; i++) {
            s += ZERO;
        }
        return in + s;
    }

    public static String dateHandle(Date date) {
        if (date == null) {
            return "";
        }
        SimpleDateFormat dateFormater = new SimpleDateFormat("yyyyMMdd");
        String strDate = dateFormater.format(date);
        return strDate;
    }

    public static void writeStringToFile(List<String> res, String batch, String name) throws IOException {

        try {
            File file = new File(name);
            //PrintStream ps = new PrintStream(new FileOutputStream(file));
            FileOutputStream fileOutputStream = new FileOutputStream(file);
            for (String str : res) {
                str += "\n";
                // ps.println(str);// 在已有的基础上添加字符串
                byte[] bs = str.getBytes("GBK");
                fileOutputStream.write(bs);
            }
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    //截取字符串 按gbk数组位置
    public static String cutByGBK(String in, int start, int length) throws UnsupportedEncodingException {
        byte[] src = in.getBytes("gbk");
        //System.out.println(s.charAt(3));
        byte[] dest = new byte[length];
        System.arraycopy(src, start, dest, 0, length);

        String res = new String(dest, "gbk");
        System.out.println(res);
        return res;
    }


	//
	/**
	 * 智能拼接sql，专门用于生成insert 语句，并能生成？,用于预定义
	 * @param map
	 * @param arg
	 * @param tableName
	 * @return
	 */
	public static String generateInsertSql(Map<String,Object> map, Object[] arg, String tableName) {
		String sql = "";
		String wh = "";
		int i=0;
		List list = new ArrayList();
		list.stream();
		
		
		for (String key : map.keySet()) {
			arg[i] = map.get(key);
			i++;
			sql += key+",";
			wh += "?,";
		}
		sql = sql.substring(0,sql.length()-1);
		wh = wh.substring(0,wh.length()-1);
		sql = " insert into "+tableName+" ( "+sql+" ) values ( "+wh+" ) "; 
		return sql;
	}
	
    public static void main(String[] args) throws UnsupportedEncodingException {
    	Map<String,Object> map = new HashMap<>();
    	
		Stream<String> stream = Stream.generate(() -> "test").limit(10);
		stream.forEach(System.out::print);
    }
}
